﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Jayden.Dll.Cards.Poker
{
	public class PokerCurrency
	{
		private string m_Name;
		private string m_IsoCode;
		private string m_Symbol;
		private int m_PowerBase;
		private string m_DisplayFormat;
		private ulong m_ChipsMultiplier = 1;

		private static Dictionary<string, PokerCurrency> m_CurrencyBySymbol = new Dictionary<string,PokerCurrency>();
		private static Dictionary<string, PokerCurrency> m_CurrencyByIsoCode = new Dictionary<string, PokerCurrency>();

		public static readonly PokerCurrency Play = new PokerCurrency("Play Money", "POK", string.Empty, 0, "###,###,###,##0", 100);
		public static readonly PokerCurrency USD = new PokerCurrency("US Dollars", "USD", "$", 2);
		public static readonly PokerCurrency EUR = new PokerCurrency("Euro", "EUR", "€", 2);

		public static PokerCurrency GetCurrencyBySymbol(string symbol)
		{
			PokerCurrency currency;
			if (m_CurrencyBySymbol.TryGetValue(symbol, out currency))
				return currency;
			return null;
		}
		public static PokerCurrency GetCurrency(string iso)
		{
			PokerCurrency currency;
			if (m_CurrencyByIsoCode.TryGetValue(iso, out currency))
				return currency;
			return null;
		}

		public PokerCurrency(string name, string iso, string symbol, int powerBase) : this(name, iso, symbol, powerBase, "###,###,###,##0.".PadRight(2 + powerBase, '0'), 1) { }
		public PokerCurrency(string name, string iso, string symbol, int powerBase, string displayFormat, ulong chipsMultiplier)
		{
			m_Name = name;
			m_IsoCode = iso;
			m_Symbol = symbol;
			m_PowerBase = powerBase;
			m_DisplayFormat = displayFormat;
			m_ChipsMultiplier = chipsMultiplier;

			m_CurrencyBySymbol.Add(symbol, this);
			m_CurrencyByIsoCode.Add(iso, this);
		}

		public string Name { get { return m_Name; } }
		public string IsoCode { get { return m_IsoCode; } }
		public string Symbol { get { return m_Symbol; } }
		public int PowerBase { get { return m_PowerBase; } }
		public string DisplayFormat { get { return m_DisplayFormat; } }
		public ulong ChipsMultiplier { get { return m_ChipsMultiplier; } }

		public string ToString(ulong value, bool withSymbol = true)
		{
			double displayValue = value / Math.Pow(10, PowerBase);
			string stringValue = displayValue.ToString(DisplayFormat, CultureInfo.InvariantCulture);
			if (!withSymbol)
				return stringValue;
			return string.Format("{0}{1}", Symbol, stringValue);
		}

		public override string ToString() { return m_IsoCode; }

		public bool TryParseAmout(string text, out ulong amount)
		{
			amount = 0;
			double doubleValue = 0.0;
			if (double.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out doubleValue))
			{
				amount = (ulong)(doubleValue * Math.Pow(10, PowerBase));
				return true;
			}
			return false;
		}


	}
}
